//
//  StudyContent.m
//  test
//1
//  Created by 朱圳 on 2018/3/8.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "StudyContent.h"

@interface StudyContent ()

@end

@implementation StudyContent

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上课详情";
    [self setLimit];
    [self setTypes];
    [self setData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)setData{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //设定时间的格式
    NSArray *p=[[Utility getDateStringWithTimeStr:self.model.startTime] componentsSeparatedByString:@" "];
    NSArray *p2=[[Utility getDateStringWithTimeStr:self.model.endTime] componentsSeparatedByString:@" "];
    self.times.text = [NSString stringWithFormat:@"%@ %@-%@",p[0],p[1],p2[1]];
    double sc = ([self.model.endTime doubleValue] - [self.model.startTime doubleValue]);
    self.courseTime.text = [NSString stringWithFormat:@"%.f 分钟",sc/60];
    self.content.text = self.model.content;
}

-(void)updateData{
    NSDictionary *dict = @{
                           @"sid" : Sid,
                           @"spass" : Spassword,
                           @"nid"  : self.model.yid,
                           @"sdisnot": self.model.sdisnot
                           };
//     NSLog(@"%@", dict);
    [[AFNetworkTool shareManager]postJSONWithUrl:Stu_Attendclass parmas:dict successData:^(id json) {
        [Utility showSuccess:json[@"message"]];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [Utility showFail:@"网络错误"];
    }];
}

/**
 重新发送请求
 */
-(void)againUpdaeData{
    NSDictionary *dict = @{
                           @"gid" : Gid,
                           @"gpass" : Gpassword,
                           @"nid"  : self.model.yid,
                           @"sdisnot": self.model.sdisnot,
                           @"content" : self.content.text
                           
                           };
//    NSLog(@"%@", dict);
    [[AFNetworkTool shareManager]postJSONWithUrl:GR_Setclass parmas:dict successData:^(id json) {
        [Utility showSuccess:json[@"message"]];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [Utility showFail:@"网络错误"];
    }];
}

/**
 type判断是从哪个界面过来的1为学生确认 2为研究生确认未上课 3为学生不符合上课
 */
-(void)setTypes{
    if ([self.type isEqualToString:@"1"]) {
        self.typesLab.text = @"学生名字:";
        self.pname.text = self.model.sname;
    }else if ([self.type isEqualToString:@"2"]) {
        self.typesLab.text = @"学生名字:";
        self.pname.text = self.model.sname;
        self.fhBtn.hidden = YES;
        self.bfhBtn.hidden = YES;
    }
    else if ([self.type isEqualToString:@"3"]) {
        self.typesLab.text = @"学生名字:";
        self.pname.text = self.model.sname;
        self.content.editable = YES;
        self.againBtn.hidden = NO;
    }else{
        self.pname.text = self.model.pname;
    }
}

/**
 设置界面显示限制
 */
-(void)setLimit{
    self.againBtn.hidden = YES;
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];

    if ([self.model.endTime doubleValue]<interval) {
        
        if ([self.model.isornot isEqualToString:@"1"]) {
            
            if ([self.model.sdisnot isEqualToString:@"1"]) {
                
                self.fhBtn.hidden = YES;
                self.bfhBtn.hidden = YES;
               
            }else if ([self.model.sdisnot isEqualToString:@"2"]){
            
                self.fhBtn.hidden = YES;
                self.bfhBtn.hidden = YES;
               
            }else{
                self.fhBtn.hidden = NO;
                self.bfhBtn.hidden = NO;
            }
        }else if ([self.model.isornot isEqualToString:@"0"]){
            self.fhBtn.hidden = YES;
            self.bfhBtn.hidden = YES;
        }else{
            self.fhBtn.hidden = YES;
            self.bfhBtn.hidden = YES;
        }
    }else{
        self.fhBtn.hidden = YES;
        self.bfhBtn.hidden = YES;
    }
}

/**
 符合

 */
- (IBAction)fhBtn:(UIButton *)sender {
    
    self.model.sdisnot = @"1";
    [self updateData];
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 拒绝

 */
- (IBAction)bfhBtn:(UIButton *)sender {
    
    self.model.sdisnot = @"2";
    [self updateData];
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 研究生重新向学生发送上课确认请求

 */
- (IBAction)againBtnClick:(UIButton *)sender {
    self.model.sdisnot = @"0";

    [self againUpdaeData];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
